package com.willian.andrappfw.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import com.berwin.cocoadialog.CocoaDialog;
import com.berwin.cocoadialog.CocoaDialogStyle;
import com.berwin.cocoadialog.CocoaDialog.Builder;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class WCustomDialogBuilder {
    private Context context;
    private int iLayoutId;
    private int iDialogWidth = -2;
    private int iDialogHeight = -2;
    private int iGravity = 8388611;
    private WCustomDialogBuilder.InitDialogViewListener mInitListener;
    private WCustomDialogBuilder.OnClickListener mOnClickListener;
    private final ArrayList<Integer> mClickableViewIdList = new ArrayList();

    public static WCustomDialogBuilder with(Context context) {
        return new WCustomDialogBuilder(context);
    }

    public WCustomDialogBuilder(Context context) {
        this.context = context;
    }

    public WCustomDialogBuilder setLayoutId(int layoutId) {
        this.iLayoutId = layoutId;
        return this;
    }

    public WCustomDialogBuilder setWidth(int width) {
        this.iDialogWidth = width;
        return this;
    }

    public WCustomDialogBuilder setHeight(int height) {
        this.iDialogHeight = height;
        return this;
    }

    public WCustomDialogBuilder setGravity(int gravity) {
        this.iGravity = gravity;
        return this;
    }

    public WCustomDialogBuilder setInitListener(WCustomDialogBuilder.InitDialogViewListener dialogInitial) {
        this.mInitListener = dialogInitial;
        return this;
    }

    public WCustomDialogBuilder setOnClickListener(WCustomDialogBuilder.OnClickListener onClickListener) {
        this.mOnClickListener = onClickListener;
        return this;
    }

    public WCustomDialogBuilder setClickableViews(Integer... clickableViewId) {
        this.mClickableViewIdList.clear();
        this.mClickableViewIdList.addAll(Arrays.asList(clickableViewId));
        return this;
    }

    public CocoaDialog create() {
        Builder builder = new Builder(this.context, CocoaDialogStyle.custom);
        View dialogView = LayoutInflater.from(this.context).inflate(this.iLayoutId, (ViewGroup)null, false);
        CocoaDialog dialog = builder.setCustomContentView(dialogView).setCustomWidth(this.iDialogWidth).setCustomHeight(this.iDialogHeight).setCustomGravity(this.iGravity).build();
        if (this.mInitListener != null) {
            this.mInitListener.initDialogView(dialog, dialogView);
        }

        if (this.mOnClickListener != null) {
            Iterator var4 = this.mClickableViewIdList.iterator();

            while(var4.hasNext()) {
                int id = (Integer)var4.next();
                View childView = dialogView.findViewById(id);
                childView.setOnClickListener((v) -> {
                    this.mOnClickListener.onClick(dialog, id);
                });
            }
        }

        return dialog;
    }

    public interface OnClickListener {
        void onClick(CocoaDialog var1, int var2);
    }

    public interface InitDialogViewListener {
        void initDialogView(CocoaDialog var1, View var2);
    }
}
